package org.edu.service;


import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;
import org.edu.common.uiltst.Md5Utils;
import org.edu.common.uiltst.UserStatic;
import org.edu.dao.EmpDao;
import org.edu.dao.RoleDao;
import org.edu.dao.UserDao;
import org.edu.dao.UserDetailedDao;
import org.edu.model.Role;
import org.edu.model.User;
import org.edu.model.UserDetailed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(rollbackFor = { RuntimeException.class }, propagation = Propagation.REQUIRED)
public class UserService extends ServiceAdapter<User, String>{
	@Autowired
	private UserDao userDao;
	@Autowired
	private UserDetailedDao userdetailedDao;
	@Autowired
	private RoleDao roleDap;
	@Autowired
	private EmpDao empDao;

	private static final Logger logger = Logger.getLogger(UserService.class);
	@Override
	public void save(User object) {
		// TODO Auto-generated method stub
		userDao.save(object);
	}

	@Override
	public void update(User object) {
		// TODO Auto-generated method stub
		userDao.update(object);
	}

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		userDao.delete(userDao.get(id));;
	}

	@Override
	public User show(String id) {
		// TODO Auto-generated method stub
		User u=userDao.get(id);
		return u;
	}
	public User getUser(String id){
		User u=userDao.get(id);
		return u;
	}
	@Override
	public List<User> list() {
		// TODO Auto-generated method stub
		return userDao.loadAll();
	}
	/**
	 * 新建用户
	 * @param user 用户属性
	 * @param ud  用户详细质料
	 * @param roleid 角色
	 * @param aidRoleid 辅助角色
	 * @param empno 部门
	 */
	public void newUser(User user,UserDetailed ud,String roleid,String aidRoleid,String empno){
		Role r= roleDap.get(roleid);
		user.setRoleid(r);
		
		Set<Role> sr= new HashSet<Role>();
		StringTokenizer tokenizer = new StringTokenizer(aidRoleid, ",");
		String s;
		while (tokenizer.hasMoreElements()) {
//			System.out.println(Integer.parseInt(tokenizer.nextElement().toString().trim()));
			s=tokenizer.nextElement().toString();
			System.out.println(s);
			sr.add(roleDap.get(s.trim()));
		}
		
		user.setSroleid(sr);
		user.setEmpid(empDao.get(Integer.parseInt(empno)));
		
		ud.setUser(user);
		
		user.setUserdetailedid(ud);
		String pass= user.getPass();
		user.setPass(Md5Utils.KL(Md5Utils.md5(pass)));//md52次加密
		userDao.save(user);
		userdetailedDao.save(ud);

	}
	/**
	 * 获取登录实例
	 * @param u 
	 * @return
	 */
	public User login(User u){
		User user= userDao.get(u.getAccount());
		if(null!=user){
//			System.out.println("登录密码："+Md5Utils.md5(u.getPass()));
//			System.out.println("用户密码："+Md5Utils.KL(user.getPass()));
			if(Md5Utils.md5(u.getPass()).equals(Md5Utils.KL(user.getPass()))){
				user.setLasttime(new Date());
				userDao.update(user);
				return user;
			}
		}
		return null;
	}
	/**
	 * 清空时长
	 * @param acc
	 */
	public void emptyTime(String acc){//,String loginuser){
		
		StringTokenizer tokenizer = new StringTokenizer(acc, ";");
		User u=null;
		String s;
		while (tokenizer.hasMoreElements()) {
			s=tokenizer.nextElement().toString().trim();
			//if(!loginuser.equals(s)){
				u=userDao.get(s);
				if(u!=null) {
					u.setLasttime(null);
				}
				userDao.update(u);
//			}else{
//				logger.error("不可清除正在登录的帐号");
//			}
		}
	}
	/**
	 * 还原默认密码
	 * @param accountarray
	 */
	public void passDefault(String accountarray) {
		StringTokenizer tokenizer = new StringTokenizer(accountarray, ";");
		User u=null;
		String s;
		while (tokenizer.hasMoreElements()) {
			s=tokenizer.nextElement().toString().trim();
			//if(!loginuser.equals(s)){
				u=userDao.get(s);
				if(u!=null) {
					u.setPass(Md5Utils.KL(Md5Utils.md5("123")));
				}
				userDao.update(u);
//			}else{
//				logger.error("不可清除正在登录的帐号");
//			}
		}
		
	}
	/**
	 * Spring 调度
	 */
	@Scheduled(fixedDelay = 10000)
	public void userExitdate(){
		UserStatic usta=UserStatic.getInit();
		//System.out.println(usta.getL().toString());
		Set<User> su= usta.getL();
		for(User u:su){
			//System.out.println(u.toString());
			logger.info(u.getAccount()+"用户离线");
			userDao.update(u);
		}
		su.clear();
		//System.out.println(usta.getL().toString());
	}

	
	
}
